package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.member.model.MemberPointsManagement;
import com.zbkj.crmeb.store.dao.ChannelIntegralConfigDao;
import com.zbkj.crmeb.store.dto.ChannelIntegralConfigDTO;
import com.zbkj.crmeb.store.model.ChannelIntegralConfig;
import com.zbkj.crmeb.store.service.ChannelIntegralConfigService;
import com.zbkj.crmeb.store.service.ChannelIntegralProductConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ChannelIntegralConfigServiceImpl extends ServiceImpl<ChannelIntegralConfigDao, ChannelIntegralConfig> implements ChannelIntegralConfigService {

    @Autowired
    private ChannelIntegralProductConfigService channelIntegralProductConfigService;

    /**
     * 功能描述: 商家积分设置保存
     * @Param: [dto]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/2/20 15:44
     */
    @Override
    public Boolean saveIntegralConfig(ChannelIntegralConfigDTO dto) {
        ChannelIntegralConfig channelIntegralConfig = this.getOne(Wrappers.<ChannelIntegralConfig>lambdaQuery()
                .eq(ChannelIntegralConfig::getMerId, dto.getMerId())
                .last("limit 1"));
        if (Objects.isNull(channelIntegralConfig)) {
            channelIntegralConfig = new ChannelIntegralConfig();
        }
        BeanUtils.copyProperties(dto, channelIntegralConfig, getNullPropertyNames(dto));
        return this.saveOrUpdate(channelIntegralConfig);
    }

    /**
     * 功能描述: 获取当前商户的积分设置
     * @Param: [merId]
     * @Return: com.zbkj.crmeb.store.dto.ChannelIntegralConfigDTO
     * @Author: wangzh
     * @Date: 2023/2/20 15:47
     */
    @Override
    public ChannelIntegralConfigDTO getIntegralConfig(Integer merId) {
        ChannelIntegralConfig channelIntegralConfig = this.getOne(Wrappers.<ChannelIntegralConfig>lambdaQuery()
                .eq(ChannelIntegralConfig::getMerId, merId)
                .last("limit 1"));
        if (Objects.isNull(channelIntegralConfig)) {
            return null;
        }
        ChannelIntegralConfigDTO channelIntegralConfigDTO = new ChannelIntegralConfigDTO();
        BeanUtils.copyProperties(channelIntegralConfig, channelIntegralConfigDTO);
        return channelIntegralConfigDTO;
    }

    /**
     * 功能描述: 获取积分商城状态
     * @Param: [merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/2/20 16:12
     */
    @Override
    public Boolean getIntegralStoreStatus(Integer merId) {
        ChannelIntegralConfig channelIntegralConfig = this.getOne(Wrappers.<ChannelIntegralConfig>lambdaQuery()
                .eq(ChannelIntegralConfig::getMerId, merId)
                .last("limit 1"));
        if (Objects.isNull(channelIntegralConfig)) {
            return false;
        }
        return channelIntegralConfig.getStatus();
    }

    /**
     * 功能描述: 开启/关闭积分商城
     * @Param: [status, merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/2/28 14:36
     */
    @Override
    public Boolean updateIntegralStoreStatus(Boolean status, Integer merId) {
        if (status) {
            // 设置检查
            if (Objects.isNull(getIntegralConfig(merId)) || CollectionUtils.isEmpty(channelIntegralProductConfigService.getList(merId))){
                throw new CrmebException("积分设置或商品设置未设置，不能开启");
            }
        }
        return this.update(Wrappers.<ChannelIntegralConfig>lambdaUpdate()
                .eq(ChannelIntegralConfig::getMerId, merId)
                .set(ChannelIntegralConfig::getStatus, status));
    }

    /**
     * 功能描述: 获取对象中为空的属性名称
     * @Param: [obj]
     * @Return: java.lang.String[]
     * @Author: wangzh
     * @Date: 2023/2/20 15:44
     */
    private String[] getNullPropertyNames (Object obj) {
        final BeanWrapperImpl src = new BeanWrapperImpl(obj);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
}
